var app = getApp()

Page({
  data: {
    category: [
      { name: '韩式', id: 'hs' },
      { name: '儿童', id: 'et' }
    ],
    detail: [
      // {
      //   'id': 'hs',
      //   'cate': '韩式',
      //   'detail': [
      //     {
      //       'thumb': 'https://img14.360buyimg.com/n0/jfs/t5779/76/5045457326/549344/845a3a99/5959dc20N02a2930d.jpg',
      //       'name': '梨花带雨'
      //     }
      //   ]
      // }
    ],
    dataList:[],
    imgList: [],
    curIndex: 0,
    hasUserInfo: false,
  },
  onReady() {},
  switchTab(e) {
    const that = this
    setTimeout(function () {
      that.setData({
        curIndex: e.target.dataset.index
      })
    }, 0)
  },
  upper() {},
  lower() {},
  //预览图片 
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var curIndex = e.target.dataset.index;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imgList[curIndex] // 需要预览的图片http链接列表  
    })
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '铂爵影楼实景装修设计',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onLoad: function (options) {
    var that = this;
    var userinfo = getApp().globalData.userInfo
    if (userinfo != null) {
      that.setData({
        hasUserInfo: true
      })
    }
    //获取数据  
    wx.request({
      url: getApp().globalData.baseURL + '/commWxApp/findProduct/api', //仅为示例，并非真实的接口地址
      data: {
        siteCode: 'www',
        catalogCode: 'hs'
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      success: function (res) {
        const result = res.data
        console.log(result)
        if (result.result == true) {
          //console.log(result.data)
          //载入数据
          that.setData({
            dataList: result.data
          })
          //将数据加入图片查看集合
          var imgs = [];
          for (var i = 0; i < that.data.dataList.length; ++i) {
            var img = [];
            var list = that.data.dataList[i]
            for (var j = 0; j < list.detail.length; ++j) {
              img.push(list.detail[j].thumb);
            }
            imgs[i] = img;
          }
          that.setData({
            imgList: imgs
          })
        }

      }
    }) 

  },
  getUserInfo: function (e) {
    var postObj = {};
    var that = this;
    //console.log(e)
    //用户信息
    var userInfo = e.detail.userInfo;
    app.globalData.userInfo = userInfo;
    postObj = Object.assign(postObj, userInfo);
    //系统信息
    wx.getSystemInfo({
      success: function (res) {
        //console.log(res)
        postObj = Object.assign(postObj, res);
        postObj.wxversion = postObj.version
        postObj.wxsystem = postObj.system
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    //获取位置信息
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        postObj = Object.assign(postObj, res);
      },
      fail: function (res) { console.log(res) },
      complete: function (res) { console.log(res) },
    })
    
    //提交数据  
    wx.request({
      url: getApp().globalData.baseURL + '/commWxApp/save/api', //仅为示例，并非真实的接口地址
      data: postObj,
      method:'POST',
      header: {
        'content-type': 'application/json', // 默认值
      },
      success: function (res) {
        console.log(res)
        
      }
    })

    that.setData({
      hasUserInfo: true
    })

  }

})